package course.cmsc436.medicationmanager;

import course.cmsc436.medication.Medication;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MedicationInfoActivity extends Activity {

	final String TAG = "MedicationInfoActivity";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.med_info);

		//all fields disabled by default in medication info activity

		Log.d(TAG, TAG);

		final Medication curMed = (Medication) getIntent().getSerializableExtra("med");
		if (curMed == null){
			Log.d(TAG, "med is null");
		}

		TextView nameDisplay = (TextView) findViewById(R.id.medInfoName);
		nameDisplay.setText(curMed.getProprietaryName());
		
		final Spinner numPills = (Spinner) findViewById(R.id.numPillSpinner);
		//number of pills coincidentally corresponds to index

		Log.d(TAG, ""+ curMed.getPillsPerDose());
		numPills.setSelection(curMed.getPillsPerDose());
		numPills.setEnabled(false);

		final Spinner pillFreq = (Spinner) findViewById(R.id.pillFreqSpinner);
		//number of times per day coincidentally corresponds to index
		pillFreq.setSelection(curMed.getTimesPerDay());
		// maybe add checked value
		pillFreq.setEnabled(false);	

		final CheckBox makeAlarm = (CheckBox) findViewById(R.id.alarmCheckbox);
		makeAlarm.setEnabled(false);


		int hours;
		int minutes;
		//time pickers for each dose
		final EditText time1 = (EditText) findViewById(R.id.doseTime1);

		Calendar c = Calendar.getInstance();

		if(curMed.getTimes().size()>=1){
			c.setTime(curMed.getTimes().get(0));
			hours = c.get(Calendar.HOUR_OF_DAY);
			minutes = c.get(Calendar.MINUTE);
			time1.setText(hours+":"+minutes);
		}
		else {
			time1.setVisibility(View.INVISIBLE);
		}
		time1.setEnabled(false);
		time1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onClickTime(time1);
			}
		});

		final EditText time2 = (EditText) findViewById(R.id.doseTime2);
		if(curMed.getTimes().size()>=2){

			c.setTime(curMed.getTimes().get(1));
			hours = c.get(Calendar.HOUR_OF_DAY);
			minutes = c.get(Calendar.MINUTE);
			time2.setText(hours+":"+minutes);
		}
		else {
			time2.setVisibility(View.INVISIBLE);
		}
		time2.setEnabled(false);
		time2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onClickTime(time2);
			}
		});

		final EditText time3 = (EditText) findViewById(R.id.doseTime3);
		if(curMed.getTimes().size()>=3){

			c.setTime(curMed.getTimes().get(2));
			hours = c.get(Calendar.HOUR_OF_DAY);
			minutes = c.get(Calendar.MINUTE);
			time3.setText(hours+":"+minutes);
		}
		else {
			time3.setVisibility(View.INVISIBLE);
		}
		time3.setEnabled(false);
		time3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onClickTime(time3);
			}
		});

		final EditText time4 = (EditText) findViewById(R.id.doseTime4);
		if(curMed.getTimes().size()>=4){
			c.setTime(curMed.getTimes().get(3));
			hours = c.get(Calendar.HOUR_OF_DAY);
			minutes = c.get(Calendar.MINUTE);
			time1.setText(hours+":"+minutes);
		}
		else {
			time4.setVisibility(View.INVISIBLE);
		}
		time4.setEnabled(false);
		time4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onClickTime(time4);
			}
		});


		pillFreq.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				//enable/disable fields
				if (pillFreq.isEnabled()) {
					changeEnableTimes(Integer.parseInt((String)pillFreq.getSelectedItem()), time1, time2, time3, time4);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				//do nothing
			}

		});

		//button for canceling add medication
		Button cancelButton = (Button) findViewById(R.id.backButton);
		cancelButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});

		//button for submit
		final Button submitButton = (Button) findViewById(R.id.submitButton);
		submitButton.setEnabled(false);
		submitButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//check to see if necessary fields are filled
				if (((String)numPills.getSelectedItem()).equals("") ||
						((String)pillFreq.getSelectedItem()).equals("")) {
					Toast msg = Toast.makeText(getApplicationContext(), "Please complete the necessary information.", Toast.LENGTH_SHORT);
					msg.show();
				}
				else {
					Intent data = new Intent();
					data.putExtra("pillsPerDose", Integer.parseInt((String)numPills.getSelectedItem()));
					data.putExtra("dosesPerDay", Integer.parseInt((String)pillFreq.getSelectedItem()));

					ArrayList<String> times = new ArrayList<String>();
					times.add(time1.getText().toString());
					times.add(time2.getText().toString());
					times.add(time3.getText().toString());
					times.add(time4.getText().toString());

					data.putExtra("doseTimes", times);
					data.putExtra("setAlarm", makeAlarm.isChecked());
					setResult(RESULT_OK, data);

					finish();
				}
			}
		});

		//enables the dosage fields and the submit button for updating medication info
		Button editButton = (Button) findViewById(R.id.editButton);
		editButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				numPills.setEnabled(true);
				pillFreq.setEnabled(true);
				submitButton.setEnabled(true);
				makeAlarm.setEnabled(true);
				//cancel old alarm
				AlarmScheduler.cancelAlarm(getApplicationContext(), curMed);
				//enable time pickers
				changeEnableTimes(Integer.parseInt((String)pillFreq.getSelectedItem()), time1, time2, time3, time4);
			}
		});
	}

	//time picker dialog appears when selecting time EditText
	private void onClickTime(final EditText timeView) {
		Calendar mCurrentTime = Calendar.getInstance();
		int hour = mCurrentTime.get(Calendar.HOUR_OF_DAY);
		int minute = mCurrentTime.get(Calendar.MINUTE);
		TimePickerDialog mTimePicker;
		mTimePicker = new TimePickerDialog(MedicationInfoActivity.this, new TimePickerDialog.OnTimeSetListener() {
			@Override
			public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
				if (selectedMinute < 10) {
					timeView.setText( selectedHour + ":0" + selectedMinute);
				}
				else {
					timeView.setText( selectedHour + ":" + selectedMinute);
				}
			}
		}, hour, minute, true);
		mTimePicker.setTitle("Select Time");
		mTimePicker.show();
	}
	
	private void changeEnableTimes(int caseVal, EditText time1, EditText time2, EditText time3, EditText time4 ) {
		switch (caseVal) {
			case 1:
				time1.setEnabled(true);
				time1.setVisibility(View.VISIBLE);
				time2.setText("");
				time2.setEnabled(false);
				time2.setVisibility(View.INVISIBLE);
				time3.setText("");
				time3.setEnabled(false);
				time3.setVisibility(View.INVISIBLE);
				time4.setText("");
				time4.setEnabled(false);
				time4.setVisibility(View.INVISIBLE);
				break;
			case 2:
				time1.setEnabled(true);
				time1.setVisibility(View.VISIBLE);
				time2.setEnabled(true);
				time2.setVisibility(View.VISIBLE);
				time3.setText("");
				time3.setEnabled(false);
				time3.setVisibility(View.INVISIBLE);
				time4.setText("");
				time4.setEnabled(false);
				time4.setVisibility(View.INVISIBLE);
				break;
			case 3:
				time1.setEnabled(true);
				time1.setVisibility(View.VISIBLE);
				time2.setEnabled(true);
				time2.setVisibility(View.VISIBLE);
				time3.setEnabled(true);
				time3.setVisibility(View.VISIBLE);
				time4.setText("");
				time4.setEnabled(false);
				time4.setVisibility(View.INVISIBLE);
				break;
			default:
				time1.setEnabled(true);
				time1.setVisibility(View.VISIBLE);
				time2.setEnabled(true);
				time2.setVisibility(View.VISIBLE);
				time3.setEnabled(true);
				time3.setVisibility(View.VISIBLE);
				time4.setEnabled(true);
				time4.setVisibility(View.VISIBLE);
				break;
		}
	}

}
